/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.ToaDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author PC
 */
public class ToaDAO {
     public static boolean Them(ToaDTO n) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO toa(so_hieu, tau, loai_toa) "
                    + "VALUES('%s','%s','%d')"
                    ,n.SoHieu,n.Tau,n.LoaiToa);
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);
        } catch (Exception ex) {
           // JOptionPane.showMessageDialog(null, "Thêm Không thành công!!!", "Thông báo", 1);
        }
        return false;
    }

    public static boolean Xoa(String id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from Toa where so_hieu = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setString(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
    
    public static ResultSet TimKiemTatCa() {
        try {
            Connection conn = DBConnect.Connect();
            String sql = "Select a.SO_HIEU, b.TEN_TAU,c.TEN\n" +
" from Toa a,Tau b ,Loai_Toa c \n" +
" where a.TAU=b.ID and a.LOAI_TOA=c.ID";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
          //  conn.close();
            return rs;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
            return null;
        }

    }
      public static boolean KiemTraTonTai(String MaNV)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Select * From toa Where MaNV like ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, MaNV);
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next())
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
       public static boolean Update(ToaDTO NV,String id)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Update toa set tau=?, loai_toa=? ,so_hieu = ? Where so_hieu = ?";           
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, NV.Tau);
            ps.setInt(2, NV.LoaiToa);
            ps.setString(3, NV.SoHieu);
            ps.setString(4,id);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
}
